﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MLM.Data;

namespace MLM.AdminCP.Class
{
    //carrier bonus
    public class BonusByMonth
    {
        public long PV { get; set; }
        public double? Bonus { get; set; }
        public int Month { get; set; }
    }
    public class BonusByYear
    {
        public int Year { get; set; }
        public List<BonusByMonth> Bonus { get; set; }
    }
    //year bonus
    public class YearBonus
    {
        public int Year { get; set; }
        public double? Bonus { get; set; }
    }
    public class BonusHelper
    {
        public static double? GroupSellings = 0;
        public static bool IsSaleHeadManager(long userId, int year, int month)
        {
            GroupSellings = 0;
            MLMDataContext ct = new MLMDataContext();
            var saleHeadManagerConfig =
                ct.Ranks.FirstOrDefault(
                    it =>
                    it.MinPVPerFirstLine > 0 && it.MinNumberOfFirstLine > 0 &&
                    (it.MinRankRequiredGroupSale > 0 || it.MinGroupSales > 0));
            var sponsorList = ct.Users.Where(it => it.SponsorID == userId).ToList();
            var startDay = new DateTime(year, month, 1, 0, 0, 0);// ngay bat dau 1 thang
            var endDay = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 0); // ngay ket thuc 1 thang
            if (saleHeadManagerConfig != null)
            {
                if (ct.Users.Count(it => it.SponsorID == userId) < saleHeadManagerConfig.MinNumberOfFirstLine)
                {
                    return false;
                }
                else
                {
                    //sponsor point
                    var activeUserCount = 0;
                    foreach (var user in sponsorList)
                    {
                        long point = 0;
                        foreach (var item in ct.Invoices.Where(p => p.UserID == user.UserId && p.Status != -1 && p.DateTimeCreated >= startDay && p.DateTimeCreated <= endDay))//datetimecreated...
                        {
                            var _point = ct.InvoiceDetails.Where(p => p.InvoiceID == item.InvoiceID);
                            if (_point.Count() > 0)
                            {
                                foreach (var a in _point)
                                {
                                    point += long.Parse(a.Point.ToString()) * long.Parse(a.Quantity.ToString());
                                }
                            }
                        }

                        if (point >= saleHeadManagerConfig.MinPVPerFirstLine)
                        {
                            activeUserCount++;
                        }
                    }
                    if (activeUserCount < saleHeadManagerConfig.MinNumberOfFirstLine)
                    {
                        return false;
                    }
                    else
                    {
                        GetGroupSelling(userId, year, month);
                        if (GroupSellings >= saleHeadManagerConfig.MinGroupSales)
                        {
                            return true;
                        }
                        else
                        {
                            foreach (var user in sponsorList)
                            {
                                long point = 0;
                                foreach (var item in ct.Invoices.Where(p => p.UserID == user.UserId && p.Status != -1 && p.DateTimeCreated >= startDay && p.DateTimeCreated <= endDay))//datetimecreated...
                                {
                                    var _point = ct.InvoiceDetails.Where(p => p.InvoiceID == item.InvoiceID);
                                    if (_point.Count() > 0)
                                    {
                                        foreach (var a in _point)
                                        {
                                            point += long.Parse(a.Point.ToString()) * long.Parse(a.Quantity.ToString());
                                        }
                                    }
                                }
                                var minPVForRank =
                                    ct.Ranks.FirstOrDefault(it => it.RankID == saleHeadManagerConfig.RankRequired).MinPV;                                
                                if (point >= minPVForRank)
                                {
                                    if (GetSubGroupSelling(0, user.UserId, year, month) >= saleHeadManagerConfig.MinRankRequiredGroupSale)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
        public static void GetGroupSelling(long userId, int year, int month)
        {
            MLMDataContext ct = new MLMDataContext();
            var sponsorList = ct.Users.Where(it => it.SponsorID == userId).ToList();
            var startDay = new DateTime(year, month, 1, 0, 0, 0);// ngay bat dau 1 thang
            var endDay = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 0); // ngay ket thuc 1 thang
            foreach (var user in sponsorList)
            {
                foreach (var item in ct.Invoices.Where(p => p.UserID == user.UserId && p.Status != -1 && p.DateTimeCreated >= startDay && p.DateTimeCreated <= endDay))//datetimecreated...
                {
                    var _point = ct.InvoiceDetails.Where(p => p.InvoiceID == item.InvoiceID);
                    if (_point.Count() > 0)
                    {
                        foreach (var a in _point)
                        {
                            GroupSellings += long.Parse(a.Point.ToString()) * long.Parse(a.Quantity.ToString());
                        }
                    }
                }
                GetGroupSelling(user.UserId, year, month);
            }
        }
        public static double? GetSubGroupSelling(double? startValue, long userId, int year, int month)
        {
            MLMDataContext ct = new MLMDataContext();
            var sponsorList = ct.Users.Where(it => it.SponsorID == userId).ToList();
            var startDay = new DateTime(year, month, 1, 0, 0, 0);// ngay bat dau 1 thang
            var endDay = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 0); // ngay ket thuc 1 thang            
            foreach (var user in sponsorList)
            {
                foreach (var item in ct.Invoices.Where(p => p.UserID == user.UserId && p.Status != -1 && p.DateTimeCreated >= startDay && p.DateTimeCreated <= endDay))//datetimecreated...
                {
                    var _point = ct.InvoiceDetails.Where(p => p.InvoiceID == item.InvoiceID);
                    if (_point.Count() > 0)
                    {
                        foreach (var a in _point)
                        {
                            GroupSellings += long.Parse(a.Point.ToString()) * long.Parse(a.Quantity.ToString());
                        }
                    }
                }
                startValue += GetSubGroupSelling(startValue, user.UserId, year, month);
            }
            return startValue;
        }
    }
}